package com.bdth.bdthmall.api.epoint.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdth.bdthmall.api.annotation.AuthToken;
import com.bdth.bdthmall.api.epoint.utils.TokenFactory;
import com.bdth.bdthmall.api.epoint.vo.ResponseTokenVo;
import org.springframework.web.bind.annotation.*;
import cn.hutool.core.util.StrUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by Administrator on 2020/1/11.
 */
@RestController
@RequestMapping("/ecservice")
public class TokenController {
    @GetMapping("/welcome")
    @AuthToken
    public String Welcome(){
        return "welcome TokenController ";
    }

    @RequestMapping(value = "/getAccessToken", method = RequestMethod.POST)
    public ResponseTokenVo login(@RequestParam String data){
        JSONObject json = JSONObject.parseObject(data);
        String token = null;
        System.out.println(json.get("appKey"));
        System.out.println(json.get("appSecret"));
        String appKey = json.getString("appKey");
        String appSecret = json.getString("appSecret");
        if(!StrUtil.isBlank(appKey) && !StrUtil.isBlank(appSecret)){
            token = TokenFactory.generatorOfMd5(appKey, appSecret);
        }
        if(token!=null) {
            return ResponseTokenVo.builder().success(true).token(token).msg("测试数据为：" + data);
        }else {
            return ResponseTokenVo.builder().success(false).token("").msg("测试数据为：" + data);
        }
    }

    @RequestMapping(value = "/getAccessToken1")
    public ResponseTokenVo getAccessToken(@RequestBody String data){
        try {
            data = URLDecoder.decode(data,"utf-8");
            System.out.println(data);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        JSONObject json = JSONObject.parseObject(data);
        data = json.getString("data");
        json = JSONObject.parseObject(data);
        String token = null;
        System.out.println(json.get("appKey"));
        System.out.println(json.get("appSecret"));
        String appKey = json.getString("appKey");
        String appSecret = json.getString("appSecret");
        if(!StrUtil.isBlank(appKey) && !StrUtil.isBlank(appSecret)){
            token = TokenFactory.generatorOfMd5(appKey, appSecret);
        }
        if(token!=null) {
            return ResponseTokenVo.builder().success(true).token(token).msg("测试数据为：" + data);
        }else {
            return ResponseTokenVo.builder().success(false).token("").msg("测试数据为：" + data);
        }
    }
}
